package com.example.gameapplication.bullet;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;

import com.example.gameapplication.BaseObject;
import com.example.gameapplication.R;
import com.example.gameapplication.database.DataUtils;
import com.example.gameapplication.plant.MainPlane;


public class EnemyBullet extends BaseObject {
    double xSpeed;//x轴的速度，正数向右，负数向左
    double ySpeed;
    BaseObject enemyPlant;
    int enemyLv;//子弹等级
    private Bitmap mBullet;
    private int currentmBulletId;
    double r = -6.0;//与我方战机的角度，默认为6

    public EnemyBullet(Resources resources, Paint paint, int enemyLv, BaseObject enemyPlant, MainPlane mainPlane, int Speed) {//子弹刚刚发出的一刻就要知道图片的倾斜角度，所以需要这些参数
        super(resources, paint);
        this.enemyLv = enemyLv;
        this.enemyPlant = enemyPlant;
        this.mainPlane = mainPlane;
        this.speed = Speed;
        setSeat(enemyPlant.midx, enemyPlant.midy);
        initEnemyBitmap();
    }
    public void setLv(int lv) {//传入当前敌机的等级，调整角度
            initEnemyBitmap();
    }

    public void initEnemyBitmap() {
        double ra;
        r = Math.asin((midx - mainPlane.midx) / (Math.sqrt(Math.pow(midx - mainPlane.midx, 2) + Math.pow(midy - mainPlane.midy, 2))));//度数
        xSpeed = Math.sin(r + Math.PI) * speed;//sin带正负 Math.PI是根据所给图片样式决定的，因为本项目默认的子弹都是向着上方的
        if (midy > mainPlane.midy) {
            ra = -r;
            ySpeed = -Math.sqrt(Math.pow(speed, 2) - Math.pow(xSpeed, 2));
        } else {
            ra = Math.PI + r;
            ySpeed = Math.sqrt(Math.pow(speed, 2) - Math.pow(xSpeed, 2));
        }
        if (mBullet != null && !mBullet.isRecycled()) mBullet.recycle();
        if (getLv()<20){
            mBullet = rotateBmp(BitmapFactory.decodeResource(resources, R.drawable.enemy_bullet), Math.toDegrees(ra));
        }else if (getLv()<40){
            mBullet = rotateBmp(BitmapFactory.decodeResource(resources, R.drawable.boss_bullet_sun_particle), Math.toDegrees(ra));
        }else if (getLv()<60){
            mBullet = rotateBmp(BitmapFactory.decodeResource(resources, R.drawable.boss_bullet_thunderball_green), Math.toDegrees(ra));
        }else if (getLv()<80){
            mBullet = rotateBmp(BitmapFactory.decodeResource(resources, R.drawable.boss_bullet_hellfire_yellow), Math.toDegrees(ra));
        }else if (getLv()<100){
            mBullet = rotateBmp(BitmapFactory.decodeResource(resources, R.drawable.boss_bullet_hellfire_red), Math.toDegrees(ra));
        }else {
            mBullet = rotateBmp(BitmapFactory.decodeResource(resources, R.drawable.my_bullet_red), Math.toDegrees(ra));
        }
        object_width = mBullet.getWidth();
        object_height = mBullet.getHeight();
    }

    @Override
    public void initBitmap() {
    }

    /**
     * 旋转bitmap
     *
     * @param b
     * @param degrees
     * @return
     */
    public static Bitmap rotateBmp(Bitmap b, double degrees) {
        if (degrees != 0 && b != null) {
            Matrix m = new Matrix();
            m.setRotate((float) degrees, (float) b.getWidth() / 2,
                    (float) b.getHeight() / 2);
            try {
                Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(),
                        b.getHeight(), m, true);
                if (b != b2) {
                    b.recycle();
                    b = b2;
                }
            } catch (OutOfMemoryError ex) {
                ex.printStackTrace();
            }
        }
        return b;
    }

    @Override
    public void drawSelf(Canvas canvas) {
        if (!isAlive()) return;
        if (mBullet==null||mBullet.isRecycled())initEnemyBitmap();

        setSeat(midx, midy);
        //画子弹
        canvas.save();
        canvas.clipRect(leftx, lefty, rightx, righty);
        canvas.drawBitmap(mBullet, leftx, lefty, paint);
        canvas.restore();
        logic();
//        attack();//攻击,转移到 DataUtils.runEnemyBulletAttack();
    }
    /**
     *  因为这种判断只要bitmap的边界接触就会死亡，但是部分bitmap的边缘是透明的,容易产生未击中已经死亡的感觉
     */
//    private void attack() {
//        if (isAlive() && mainPlane.unbeatable <= 0 && rightx > mainPlane.leftx && leftx < mainPlane.rightx && righty > mainPlane.lefty && lefty < mainPlane.righty) {
//            //击中mainPlane了
//            setAlive(false);//子弹死亡
//            if (mainPlane.lv > 1) {//最低1级，伤害是等级的倍数，所以不可以为0,也可以看着0生命值就是死亡
//                mainPlane.lv--;
//                mainPlane.unbeatable = 100;
//            } else {
//                mainPlane.setAlive(false);
//            }
//        }
//    }

    private void attack() {
       DataUtils.attack(this,mainPlane);
    }
    public void logic() {//子弹的逻辑，产生之后，x不变，y轴变化

        if (midy < 0 || midy > mainPlane.screen_height || midx < 0 || midx > mainPlane.screen_width) {//离开屏幕
            setAlive(false);
            r = -6.0;
            return;
        }
        if (isAlive())//子弹击中目标，则在离开屏幕之前就被销毁了
        {
            midy = (float) (midy + ySpeed);
            midx = (float) (midx + xSpeed);
        }

    }

    @Override
    public void setSeat(float x, float y) {
        super.setSeat(x, y);
        if (r == -6.0) {
            r = Math.asin((midx - mainPlane.midx) / (Math.sqrt(Math.pow(midx - mainPlane.midx, 2) + Math.pow(midy - mainPlane.midy, 2))));//度数
            xSpeed = -Math.sin(r) * speed;//sin带正负
            if (midy > mainPlane.midy) {
                ySpeed = -Math.sqrt(Math.pow(speed, 2) - Math.pow(xSpeed, 2));
            } else {
                ySpeed = Math.sqrt(Math.pow(speed, 2) - Math.pow(xSpeed, 2));
            }
        }
    }

    @Override
    public void dataDestroyed() {
        if (!mBullet.isRecycled()) {
            mBullet.recycle();
        }
    }
}
